<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/18
 * Time: 9:30
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Zhengce extends Base
{
    /**
     * 年份列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['zc_id'] = 3; // 二期
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('zhengce')->where($where)->count();// 获取总条数
            $lists = Db::name('zhengce')
                ->field('zc_id, zc_year, zc_is_show, zc_update_time, zc_link')
                ->where($where)
                ->order('zc_year asc')
                ->page($NowPage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function zcEdit(){
        $zc_id = input('zc_id');
        if(Request::instance()->isPost()){
//            $zc_year = input('zc_year');
//            $have = Db::table('up_zhengce')
//                ->where('zc_year', $zc_year)
//                ->where('zc_id', 'NEQ', $zc_id)
//                ->find();
//            if($have){
//                return json(['code' => 100, 'msg' => '年份重复']);
//            }
//            $update = [
//                'zc_year' => $zc_year,
//                'zc_update_time' => date('Y-m-d H:i:s')
//            ];
//            $res = Db::table('up_zhengce')->where('zc_id', $zc_id)->update($update);
            $link = trim(input('zc_link'), ' ');
            $update = [
                'zc_link' => $link,
                'zc_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_zhengce')->where('zc_id', $zc_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
//        $year = Db::table('up_zhengce')->where('zc_id', $zc_id)->value('zc_year');
        $zc_link = Db::table('up_zhengce')->where('zc_id', $zc_id)->value('zc_link');
        $this->assign([
//           'year' => $year,
           'zc_link' => $zc_link,
           'zc_id' => $zc_id
        ]);
        return $this->fetch('zhengce/zcedit');
    }
    
    /**
     * 是否显示
     * @return \think\response\Json
     */
    public function zcStatus(){
        $zc_id = input('id');
        $is_show = Db::table('up_zhengce')->where('zc_id', $zc_id)->value('zc_is_show');
        if($is_show == 1){
            $zc_is_show = 2;
        }else{
            $zc_is_show = 1;
        }
        $res = Db::table('up_zhengce')->where('zc_id', $zc_id)->setField('zc_is_show', $zc_is_show);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 政策说明列表
     * @return mixed|\think\response\Json
     */
    public function zcExplain(){
        $zc_id = input('zc_id');
        if($zc_id != ''){
            session('zc_id', $zc_id);
        }
        if(request()->isAjax()){
            $where['explain_fid'] = session('zc_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('zc_explain')->where($where)->count();// 获取总条数
            $lists = Db::name('zc_explain')
                ->field('explain_id, explain_name, explain_update_time')
                ->where($where)
                ->order('explain_update_time desc')
                ->page($NowPage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('zhengce/zcexplain');
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function explainDetail(){
        $explain_id = input('explain_id');
        $explain_content = Db::table('up_zc_explain')->where('explain_id', $explain_id)->value('explain_content');
        $this->assign('content', $explain_content);
        return $this->fetch('zhengce/explaindetail');
    }
    
    /**
     * 编辑说明
     * @return mixed|\think\response\Json
     */
    public function explainEdit(){
        $explain_id = input('explain_id');
        if(Request::instance()->isPost()){
            $explain_name = input('explain_name');
            $explain_content = input('explain_content');
            $update = [
                'explain_name' => $explain_name,
                'explain_content' => $explain_content,
                'explain_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_zc_explain')->where('explain_id', $explain_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_zc_explain')
            ->field('explain_name, explain_content')
            ->where('explain_id', $explain_id)
            ->find();
        $this->assign([
           'info' => $info,
            'explain_id' => $explain_id
        ]);
        return $this->fetch('zhengce/explainedit');
    }
}